package com.gorkr.labuladong.data_struct.bst;

/**
 * @author gorkr
 * @date 2022/05/28 00:44
 **/
public class M1038BinarySearchTreeToGreaterSumTree {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public TreeNode bstToGst(TreeNode root) {
        traverse(root);
        return root;

    }
    private Integer sum = 0;
    private Integer before = 0;

    // 中序遍历的逆
    public void traverse(TreeNode root){
        if(root==null){
            return;
        }
        traverse(root.right);
        before=sum;
        sum+= root.val;
        root.val = before;
        traverse(root.left);
    }
}
    